/*
 * AccountPanel.cpp
 *
 *  Created on: 14.07.2009
 *      Author: h3llfire
 */

#include "AccountPanel.h"
#include "AccountSettingsFrame.h"
#include <vector>

namespace Impp {

BEGIN_EVENT_TABLE(AccountPanel, wxPanel)
	EVT_LIST_ITEM_ACTIVATED(wxID_ANY, AccountPanel::onItemActivated)
	EVT_BUTTON(ID_NEW_ACCOUNT, AccountPanel::onNewAccount)
	EVT_BUTTON(ID_EDIT_ACCOUNT, AccountPanel::onEditAccount)
	EVT_BUTTON(ID_DELETE_ACCOUNT, AccountPanel::onDeleteAccount)
END_EVENT_TABLE()

AccountPanel::AccountPanel(wxWindow* parent) :
	wxPanel(parent){

	wxBoxSizer* topSizer = new wxBoxSizer( wxVERTICAL );
	wxBoxSizer* buttonSizer = new wxBoxSizer( wxHORIZONTAL );

	list = new wxListCtrl(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT|wxLC_HRULES|wxLC_VRULES|wxBORDER_SUNKEN);

	wxListItem protocolType;
	protocolType.SetText(wxT("Protocol"));
	protocolType.SetImage(-1);
	list->InsertColumn(0, protocolType);

	wxListItem screenName;
	screenName.SetText(wxT("Screenname"));
	screenName.SetImage(-1);
	list->InsertColumn(1, screenName);

	std::vector<Account*> accounts = Impp::exemplar()->getAccounts();

	for(size_t i = 0; i < accounts.size(); i++){
		Account* acc = accounts[i];


		long itemId = list->InsertItem(i,wxT("ICQ"));

		list->SetItemPtrData(itemId, (wxUIntPtr)acc);
		list->SetItem(itemId,1,acc->self->getScreenName());

	}

	list->SetColumnWidth( 0, 100);
	list->SetColumnWidth( 1, 200);

	wxButton* btNew = new wxButton(this, ID_NEW_ACCOUNT, wxT("New Account"));
	wxButton* btEdit = new wxButton(this, ID_EDIT_ACCOUNT, wxT("Edit Account"));
	wxButton* btDelete = new wxButton(this, ID_DELETE_ACCOUNT, wxT("Delete Account"));

	buttonSizer->Add(btNew, 1, wxALL, 2);
	buttonSizer->Add(btEdit, 1, wxALL, 2);
	buttonSizer->Add(btDelete, 1, wxALL, 2);

	topSizer->Add(list,1, wxGROW|wxALIGN_CENTRE|wxALL, 5 );
	topSizer->Add(buttonSizer, 0, wxALL, 2);
	SetSizer(topSizer);
	Layout();
}

AccountPanel::~AccountPanel() {

}

void AccountPanel::onItemActivated(wxListEvent & event){
	Account* acc = (Account*)list->GetItemData(event.GetIndex());
	AccountSettingsFrame* settings = new AccountSettingsFrame(this, acc);
	settings->Show();
	//wxLogDebug(wxT("id: %i"), acc->id.ToLong());
}

void AccountPanel::onNewAccount(wxCommandEvent & event){
	AccountSettingsFrame settings(this, NULL);
	settings.ShowModal();
}

void AccountPanel::onEditAccount(wxCommandEvent & event){
	Account* acc = getSelectedAccount();
	if(acc == NULL){
		wxLogDebug(wxT("AccountPanel::onEditAccount: No Account selected"));
		return;
	}
	AccountSettingsFrame settings(this, acc);
	settings.ShowModal();
}

void AccountPanel::onDeleteAccount(wxCommandEvent & event){
	Account* acc = getSelectedAccount();
	if(acc == NULL){
		wxLogDebug(wxT("AccountPanel::onDeleteAccount: No Account selected"));
		return;
	}
	Impp::exemplar()->deleteAccount(acc->id);

}

Account* AccountPanel::getSelectedAccount(){
	long itemIndex = -1;
    itemIndex = list->GetNextItem(itemIndex, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
    if (itemIndex == -1){
    	return NULL;
    }
    return (Account*)list->GetItemData(itemIndex);
}

}
